/**
 * @file /include/gui/qnode.hpp
 *
 * @brief Communications central!
 *
 * @date February 2011
 **/
/*****************************************************************************
** Ifdefs
*****************************************************************************/

#ifndef gui_QNODE_HPP_
#define gui_QNODE_HPP_

/*****************************************************************************
** Includes
*****************************************************************************/

// To workaround boost/qt4 problems that won't be bugfixed. Refer to
//    https://bugreports.qt.io/browse/QTBUG-22829
#ifndef Q_MOC_RUN
#include <ros/ros.h>
#endif
#include <string>
#include <QThread>
#include <QStringListModel>

#include "std_msgs/Int32.h"
#include "std_msgs/String.h"
#include "qmutex.h"

//
#include <agv_msgs/agv_loc_cmd.h>
#include "agv_msgs/arm_operation_info.h"
#include "agv_msgs/arm_run_info.h"
#include "agv_msgs/battary_thold.h"
#include "agv_msgs/device_status.h"
#include "agv_msgs/error_clear.h"
#include "agv_msgs/mech_param.h"
#include "agv_msgs/mission_info.h"
#include "agv_msgs/paw_control.h"
#include "agv_msgs/radar_amend.h"
#include "agv_msgs/sensor_cmd.h"
#include "agv_msgs/screen_param.h"
#include "agv_msgs/running_info.h"
#include "agv_msgs/io_send.h"
#include "agv_msgs/motor_info_re.h"
#include "agv_msgs/battery_info.h"
#include "agv_msgs/control_info_send.h"
#include "agv_msgs/mask.h"
#include "agv_msgs/screen_param.h"
#include "agv_msgs/pc_robot_status_run.h"
#include "agv_msgs/send_mech_para.h"
//#include "agv_msgs/agvsw_ts_send.h"
#include "agv_msgs/agvswstate.h"
#include "agv_msgs/communication_check.h"
#include "agv_msgs/log_info.h"
#include "agv_msgs/arm_cmd.h"
#include "agv_msgs/quick_fixed_position_send.h"
#include "agv_msgs/radar_bucket_info.h"
#include "agv_msgs/gyro_s.h"
#include "agv_msgs/selftask_info.h"
#include "agv_msgs/mission_cancel.h"
#include "agv_msgs/robot_pause.h"
#include "agv_msgs/pc_info.h"
#include "agv_msgs/sensor_control.h"
#include "agv_msgs/load_upload_cargo.h"
#include "agv_msgs/led_show.h"
#include "agv_msgs/sys_config.h"
#include "agv_msgs/ecoline_info.h"
#include "std_msgs/Int32MultiArray.h"
#include "agv_msgs/self_task_cmd.h"
/*****************************************************************************
** Namespaces
*****************************************************************************/

namespace gui {

/*****************************************************************************
** Class
*****************************************************************************/

class QNode : public QThread {
    Q_OBJECT
public:
	QNode(int argc, char** argv );
	virtual ~QNode();
	bool init();
	bool init(const std::string &master_url, const std::string &host_url);
	void run();

	/*********************
	** Logging
	**********************/
	enum LogLevel {
	         Debug,
	         Info,
	         Warn,
	         Error,
	         Fatal
	 };

	QStringListModel* loggingModel() { return &logging_model; }
	void log( const LogLevel &level, const std::string &msg);

Q_SIGNALS:
	void loggingUpdated();
    void rosShutdown();
    void guiShutdown();

private:
	int init_argc;
	char** init_argv;
	ros::Publisher chatter_publisher;
    QStringListModel logging_model;

    //add topic 接收
    ros::Subscriber pc_robot_status_run_re_sub_;
    ros::Subscriber agv_loc_info_sub_;
    ros::Subscriber arm_run_info_sub_;
    ros::Subscriber mechanical_param_sub_;
    ros::Subscriber arm_operation_info_sub_;
    ros::Subscriber mission_info_sub_;
    ros::Subscriber device_status_sub_;
    ros::Subscriber agv_running_info_sub_;
    ros::Subscriber debug_sub_;
    ros::Subscriber io_send_sub_;
    ros::Subscriber motor_info_re_sub_;
    ros::Subscriber battery_info_re_sub_;
    ros::Subscriber screen_para_sub_;
    ros::Subscriber communication_check_sub_;
    ros::Subscriber version_sub_;
    ros::Subscriber agv_loc_status_sub_;
    ros::Subscriber agvsw_status_sub_;
    ros::Subscriber log_info_sub_;
    ros::Subscriber radar_bucket_info_sub_;
    ros::Subscriber network_info_sub_;
    ros::Subscriber gyro_info_sub_;
    ros::Subscriber selftask_info_sub_;
    ros::Subscriber remote_control_re_sub_;
    ros::Subscriber pc_info_re_sub_;
    ros::Subscriber sys_config_re_sub_;
    ros::Subscriber ecoline_info_sub_;
    ros::Subscriber communication_check_mianbao_sub_;


    void pcRobotStatusRunCallback(agv_msgs::pc_robot_status_run msg);
    void agvLocInfoCallback(agv_msgs::agv_loc_cmd msg);
    void screenParaCallback(agv_msgs::screen_param msg);
    void debugCallback(const std_msgs::Int32::ConstPtr& msg);
    void mechParamCallback(agv_msgs::mech_param msg);
    void armRunInfoCallback(const agv_msgs::arm_run_info::ConstPtr& msg);
    void armOperationInfoCallback(agv_msgs::arm_operation_info msg);
    void missionInfoCallback(agv_msgs::mission_info msg);
    void deviceStatusCallback(agv_msgs::device_status msg);
    void agvRunningInfoCallback(agv_msgs::running_info msg);
    void ioSendCallback(agv_msgs::io_send msg);
    void motorInforeCallback(agv_msgs::motor_info_re msg);
    void batteryInforeCallback(agv_msgs::battery_info msg);
    void CommunicationcCheckCallback(agv_msgs::communication_check msg);
    void CommunicationcCheckMianBaoCallback(const std_msgs::Int32 msg);
    void versionCallback(const std_msgs::String msg);
    void agvLocStatusCallback(const std_msgs::Int32 msg);
    void agvSwStatusCallback(agv_msgs::agvswstate msg);
    void logInfoCallback(agv_msgs::log_info msg);
    void radarBucketInfoCallback(agv_msgs::radar_bucket_info msg);
    void networkInfoStatusCallback(const std_msgs::Int32 msg);
    void gyroInfoCallback(agv_msgs::gyro_s msg);
    void selftaskInfocallback(agv_msgs::selftask_info msg);
    void remoteControlCallback(const std_msgs::Int32 msg);
    void pcInfoCallback(agv_msgs::pc_info msg);
    void sysConfigCallback(agv_msgs::sys_config msg);
    void ecolineInfoCallback(agv_msgs::ecoline_info msg);


    //end topic
    //下发
    ros::Publisher control_send_pub_;
    ros::Publisher mech_param_pub_;
    ros::Publisher errmask_send_pub_;
    ros::Publisher arm_open_close_pub_;
    ros::Publisher quick_fixed_position_send_pub_;
    ros::Publisher mission_cancel_send_pub_;
    ros::Publisher robot_pause_send_pub_;

    ros::Publisher cancel_task_send_pub_;
    ros::Publisher reboot_agv_send_pub_;
    ros::Publisher qt_jackup_operate_pub_;
    ros::Publisher qt_up_down_pub_;
    ros::Publisher load_upload_cargo_pub_;
    ros::Publisher led_show_send_pub_;
    ros::Publisher sys_config_send_pub_;
    ros::Publisher clean_mode_send_pub_;
    ros::Publisher self_task_cmd_pub_;

  //  void controlSendCallback(agv_msgs::control_info_send msg);
//    void pcRobotStatusRunCallback(agv_msgs::pc_robot_status_run msg);
//    void agvLocInfoCallback(agv_msgs::agv_loc_cmd msg);

public:
  //sub data
    agv_msgs::pc_robot_status_run usb_robot_status_run_re_;
    agv_msgs::agv_loc_cmd agv_loc_info_re_;
    agv_msgs::screen_param screen_para_re_;
    agv_msgs::arm_run_info arm_run_info_re_;
    agv_msgs::mech_param mech_param_;
    agv_msgs::arm_operation_info arm_operation_info_;
    agv_msgs::mission_info mission_info_;
    agv_msgs::device_status device_status_;
    agv_msgs::running_info running_info_;
    agv_msgs::io_send io_send_;
    agv_msgs::io_send io_send_IO;
    agv_msgs::io_send io_send_ultr;

    agv_msgs::motor_info_re motor_info_re_;
    agv_msgs::motor_info_re motor_info_re_Run[6];
    agv_msgs::motor_info_re motor_info_re_Od;
    agv_msgs::motor_info_re motor_info_re_Hy;
    agv_msgs::agvswstate agvsw_status_re_;
    agv_msgs::battery_info battery_info_re_;
    agv_msgs::log_info log_info_re_;
    agv_msgs::radar_bucket_info radar_bucket_info_re_;
    agv_msgs::gyro_s gyro_info_re_;
    agv_msgs::selftask_info selftask_info_re;
    agv_msgs::pc_info pc_info_re_;
    agv_msgs::sys_config sys_config_re_;
    agv_msgs::sys_config sys_config_send_;
    agv_msgs::ecoline_info ecoline_info_re_;
    agv_msgs::ecoline_info ecoline_info_re_num_[3];

    agv_msgs::communication_check communication_check_re_;
    std_msgs::Int32 communication_check_mianbao_re_;
    std_msgs::Int32 agv_loc_info;
    std_msgs::String version;
    std_msgs::Int32 network_info_re_;
    std_msgs::Int32 remote_control_re_;

    //send data
    agv_msgs::send_mech_para send_param_;
    agv_msgs::control_info_send control_info_send_;
    agv_msgs::mask errmask_send_;
    agv_msgs::arm_cmd arm_cmd_send_;
    agv_msgs::quick_fixed_position_send quick_fixed_position_send_;
    agv_msgs::mission_cancel mission_cancel_send_;
    agv_msgs::robot_pause robot_pause_send_;
    agv_msgs::sensor_control qt_up_down_send_;
    agv_msgs::load_upload_cargo load_upload_cargo_send_;
    agv_msgs::led_show led_show_send_;
    agv_msgs::self_task_cmd self_task_cmd_send_;

    std_msgs::Int32 cancel_task_send_;
    std_msgs::Int32 reboot_agv_send_;
    std_msgs::Int32 qt_jackup_operate_send_;
    std_msgs::Int32MultiArray clean_mode_send_;


    void MechParamPublish(agv_msgs::send_mech_para msg);
    void controlSendPublish(agv_msgs::control_info_send msg);
    void errmaskSendPublish(agv_msgs::mask msg);
    void armcmdSendPublish(agv_msgs::arm_cmd msg);
    void quickFixedPositionSendPublish(agv_msgs::quick_fixed_position_send msg);
    void missionCancelSendCallback(agv_msgs::mission_cancel msg);
    void robotPauseSendCallback(agv_msgs::robot_pause msg);
    void QtUpDownPublish(agv_msgs::sensor_control msg);
    void LoadUploadCargoPulish(agv_msgs::load_upload_cargo msg);
    void ledShowSendPublish(agv_msgs::led_show msg);
    void sysConfigSendPublish(agv_msgs::sys_config msg);
    void selfTaskSendPublish(agv_msgs::self_task_cmd msg);

    void cancelTaskSendPublish(const std_msgs::Int32 msg);
    void rebootAgvSendPublish(const std_msgs::Int32 msg);
    void QtJackupOperatePublish(const std_msgs::Int32 msg);
    void cleanModeSendPublish(const std_msgs::Int32MultiArray msg);

    int from_arm_check_connection_;
    int from_arm_check_connection_last_;

    QMutex mutex;
};

}  // namespace gui

#endif /* gui_QNODE_HPP_ */
